﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace _12.RemoveListedWords
{
    class RemoveListedWords
    {
        static void Main()
        {
            List<string> removeList = new List<string>();
            try
            {
                using (StreamReader readerRemoveList = new StreamReader("RemoveList.txt", System.Text.Encoding.Unicode))
                {
                    string line = readerRemoveList.ReadLine();
                    while (line != null)
                    {
                        foreach (Match match in Regex.Matches(line, @"[\w*\p{S}*]+"))
	                    {
		                    removeList.Add(match.Value);
	                    }
                        line = readerRemoveList.ReadLine();
                    }
                }
            }
            catch (Exception)
            {
                Console.WriteLine("The list with words to remove can not be read.");
            }

            string text = "";
            try
            {
                using (StreamReader readerInput = new StreamReader("Text.txt", System.Text.Encoding.Unicode))
                {
                    string line = readerInput.ReadLine();
                    while (line != null)
                    {
                        text += line + "\r\n";
                        line = readerInput.ReadLine();
                    }
                }
            }
            catch (Exception)
            {
                Console.WriteLine("The text file can not be read.");
            }
            
            try
            {
                foreach (var removeWord in removeList)
                {
                    text = Regex.Replace(text, "\\b" + removeWord + "\\b", string.Empty);
                }
                text = text.Replace("  ", string.Empty);

                using (StreamWriter writerOutput = new StreamWriter("Text.txt", false, System.Text.Encoding.Unicode))
                {
                    writerOutput.Write(text);
                }
            }
            catch (Exception)
            {
                Console.WriteLine("Error occured during removing words");
            }
        }
    }
}
